/*
 * Copyright 2010-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.newpath.core.impl;

import java.util.Iterator;
import java.util.List;

import com.google.code.newpath.core.Context;
import com.google.code.newpath.core.Path;
import com.google.code.newpath.core.Resource;
import com.google.code.newpath.core.ResourceCreator;
import com.google.code.newpath.core.ResourceFactory;
import com.google.code.newpath.core.ResourceInstanceEvent;
import com.google.code.newpath.core.ResourceInstanceProcessor;
import com.google.code.newpath.core.exception.ResourceInstanceException;

/**
 * 
 * @author Charlie Zhang
 *
 */
public abstract class BaseResource<T> implements Resource<T> {
	
	private Path path;
	
	private String scope;
	
	private ResourceCreator<?> creator;
	
	private final List<ResourceInstanceEvent> events;
	
	protected BaseResource(final List<ResourceInstanceEvent> events) {
		this.events = events;
	}

	public Path getPath() {
		return path;
	}

	public String getScope() {
		return scope;
	}

	public ResourceCreator<?> getCreator() {
		return creator;
	}

	public void setCreator(ResourceCreator<?> creator) {
		this.creator = creator;
	}

	public void setPath(Path path) {
		this.path = path;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}

	public List<ResourceInstanceEvent> getInstanceEvents() {
		return events;
	}
	
	

	public T instanceTarget(Context context) throws ResourceInstanceException {
		T target;
		if(events != null) {
			final Iterator<ResourceInstanceEvent> it = events.iterator();
			final Resource thisResource = this;
			final ResourceInstanceProcessor<T> processor = new ResourceInstanceProcessor<T>() {
				public T process(Context context) {
					if(it.hasNext()) {
						ResourceInstanceEvent<T> event = it.next();
						return event.instanceTarget(this, context, thisResource);
					}
					else {
						return doInstanceTarget(context);
					}
				}
			};
			
			
			target = processor.process(context);
		}
		else {
			target = doInstanceTarget(context);
		}
		
		afterInstanceTarget(context, target);
		
		return target;
	}

	/**
	 * 
	 * Do actually target instance action.
	 * 
	 * @param context
	 * @return
	 */
	protected abstract T doInstanceTarget(Context context);
	
	/**
	 * Will invoke just after the target has been instanced.
	 * @param context
	 * @return
	 */
	protected abstract void afterInstanceTarget(Context context, Object target);
}
